from typing import Dict, Any
from .error_types import ErrorType

class ErrorClassifier:
    """错误分类器"""
    
    @staticmethod
    def classify_error(exception: Exception, context: Dict[str, Any] = None) -> ErrorType:
        """根据异常类型和上下文对错误进行分类"""
        
        error_message = str(exception).lower()
        
        # 网络相关错误
        if any(keyword in error_message for keyword in [
            'connection', 'network', 'timeout', 'unreachable', 'dns'
        ]):
            return ErrorType.NETWORK_ERROR
        
        # API限流错误
        if any(keyword in error_message for keyword in [
            'rate limit', 'too many requests', '429', 'throttling'
        ]):
            return ErrorType.API_RATE_LIMIT
        
        # 认证错误
        if any(keyword in error_message for keyword in [
            'unauthorized', '401', 'authentication', 'invalid token', 'expired'
        ]):
            return ErrorType.AUTH_ERROR
        
        # 配额超限
        if any(keyword in error_message for keyword in [
            'quota exceeded', 'limit exceeded', 'insufficient credits'
        ]):
            return ErrorType.QUOTA_EXCEEDED
        
        # 服务不可用
        if any(keyword in error_message for keyword in [
            'service unavailable', '503', 'server error', '500', 'internal error'
        ]):
            return ErrorType.SERVICE_UNAVAILABLE
        
        # 内容过滤
        if any(keyword in error_message for keyword in [
            'content policy', 'safety', 'inappropriate', 'blocked'
        ]):
            return ErrorType.CONTENT_FILTER
        
        # 参数验证错误
        if any(keyword in error_message for keyword in [
            'validation', 'invalid parameter', 'bad request', '400'
        ]):
            return ErrorType.VALIDATION_ERROR
        
        # 超时错误
        if any(keyword in error_message for keyword in [
            'timeout', 'timed out'
        ]):
            return ErrorType.TIMEOUT
        
        return ErrorType.UNKNOWN_ERROR

    @staticmethod
    def get_retry_strategy(error_type: ErrorType) -> str:
        """根据错误类型获取建议的重试策略"""
        retry_strategies = {
            ErrorType.NETWORK_ERROR: "exponential",
            ErrorType.API_RATE_LIMIT: "exponential",
            ErrorType.SERVICE_UNAVAILABLE: "exponential", 
            ErrorType.TIMEOUT: "linear",
            ErrorType.AUTH_ERROR: "no_retry",
            ErrorType.QUOTA_EXCEEDED: "no_retry",
            ErrorType.VALIDATION_ERROR: "no_retry",
            ErrorType.CONTENT_FILTER: "no_retry",
            ErrorType.UNKNOWN_ERROR: "linear"
        }
        return retry_strategies.get(error_type, "no_retry")

    @staticmethod
    def is_recoverable(error_type: ErrorType) -> bool:
        """判断错误是否可恢复"""
        recoverable_errors = {
            ErrorType.NETWORK_ERROR,
            ErrorType.API_RATE_LIMIT, 
            ErrorType.SERVICE_UNAVAILABLE,
            ErrorType.TIMEOUT,
            ErrorType.UNKNOWN_ERROR
        }
        return error_type in recoverable_errors